package com.comac.print.component.app.controller;

import cc.comac.techfoundation.audit.annotation.OptLog;
import cc.comac.techfoundation.audit.enums.OptType;
import com.alibaba.cola.dto.Response;
import com.alibaba.fastjson.JSON;
import com.comac.print.component.app.api.PrintService;
import com.comac.print.component.app.common.MaterPlateCommon;
import com.comac.print.component.app.common.PrintContext;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.text.SimpleDateFormat;

/**
 * @Description 打印控制器
 * @Author yy
 * @Date 2022/4/14 17:35
 */
@Api("打印控制器")
@RestController
@Slf4j
public class PrintController {

    @Autowired
    private PrintService printService;


    @ApiOperation(value = "打印")
    @PostMapping(value = "/print")
    @OptLog(optType = OptType.INSERT, detail = "打印接口调用,模板名称：{#printContext.systemId}")
    public Response print(@RequestBody PrintContext printContext) throws Exception {
        log.info("print.params: {}", JSON.toJSONString(printContext));
        return printService.print(printContext);
    }

    @ApiOperation(value = "模板上传")
    @PostMapping(value = "/uploadFile",consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    @OptLog(optType = OptType.INSERT_OR_UPDATE, detail = "打印模板上传,模板名称：{#systemId}")
    public Response uploadFile(@RequestParam(value = "systemId",required = true)String systemId,
                               @RequestParam("bussinessId")String bussinessId,
                               @RequestParam("version")String version,
                               @RequestParam("validStart") String validStart,
                               @RequestParam("validEnd")String validEnd,
                               @RequestParam("operator")String operator,
                               @RequestPart(value = "file") MultipartFile file) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        MaterPlateCommon materPlateCommon = new MaterPlateCommon();
        materPlateCommon.setSystemId(systemId);
        materPlateCommon.setVersion(version);
        materPlateCommon.setBussinessId(bussinessId);
        materPlateCommon.setValidEnd(format.parse(validEnd));
        materPlateCommon.setValidStart(format.parse(validStart));
        materPlateCommon.setOperator(operator);
        materPlateCommon.setFile(file);
        return printService.uploadFile(materPlateCommon);
    }

}
